package com.tiheima.service.impl;

import com.tiheima.mapper.OperateLogMapper;
import com.tiheima.model.entity.OperateLog;
import com.tiheima.service.OperationLogService;
import com.tiheima.utils.ThreadLocalUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class OperationLogServiceImpl implements OperationLogService {

    @Autowired
    private OperateLogMapper operateLogMapper;

    @Async
    @Override
    public void insertLog(ProceedingJoinPoint pjp, Object returnValue, Integer empId, long costTime) throws InterruptedException {
        // 记录增删改接口操作日志到  operate_log 表中
        OperateLog operateLog = new OperateLog();
        //1.1执行耗时
        operateLog.setCostTime(costTime);
        // 1.2获取操作时间
        LocalDateTime operateTime = LocalDateTime.now();
        operateLog.setOperateTime(operateTime);
        // 1.3 获取操作类名
        String className = pjp.getTarget().getClass().getName();
        operateLog.setClassName(className);
        // 1.4 获取操作方法名
        String methodName = pjp.getSignature().getName();
        operateLog.setMethodName(methodName);
        //1.5 获取方法参数
        Object[] args = pjp.getArgs();
        operateLog.setMethodParams(Arrays.toString(args));
        // 1.6 获取返回值
        operateLog.setReturnValue(returnValue.toString());
        // 1.7 获取操作人id
        // 从ThreadLocal中获取
        operateLog.setOperateEmpId(empId);
        // 异步处理！
        operateLogMapper.insert(operateLog);
    }
}
